/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import test.entities.Client;
import test.entities.PortefeuilleAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import test.controllers.exceptions.IllegalOrphanException;
import test.controllers.exceptions.NonexistentEntityException;
import test.controllers.exceptions.PreexistingEntityException;
import test.entities.Portefeuille;

/**
 *
 * @author Issam
 */
public class PortefeuilleJpaController implements Serializable {

    public PortefeuilleJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Portefeuille portefeuille) throws PreexistingEntityException, Exception {
        if (portefeuille.getPortefeuilleActionCollection() == null) {
            portefeuille.setPortefeuilleActionCollection(new ArrayList<PortefeuilleAction>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Client email = portefeuille.getEmail();
            if (email != null) {
                email = em.getReference(email.getClass(), email.getEmail());
                portefeuille.setEmail(email);
            }
            Collection<PortefeuilleAction> attachedPortefeuilleActionCollection = new ArrayList<PortefeuilleAction>();
            for (PortefeuilleAction portefeuilleActionCollectionPortefeuilleActionToAttach : portefeuille.getPortefeuilleActionCollection()) {
                portefeuilleActionCollectionPortefeuilleActionToAttach = em.getReference(portefeuilleActionCollectionPortefeuilleActionToAttach.getClass(), portefeuilleActionCollectionPortefeuilleActionToAttach.getPortefeuilleActionPK());
                attachedPortefeuilleActionCollection.add(portefeuilleActionCollectionPortefeuilleActionToAttach);
            }
            portefeuille.setPortefeuilleActionCollection(attachedPortefeuilleActionCollection);
            em.persist(portefeuille);
            if (email != null) {
                email.getPortefeuilleCollection().add(portefeuille);
                email = em.merge(email);
            }
            for (PortefeuilleAction portefeuilleActionCollectionPortefeuilleAction : portefeuille.getPortefeuilleActionCollection()) {
                Portefeuille oldPortefeuilleOfPortefeuilleActionCollectionPortefeuilleAction = portefeuilleActionCollectionPortefeuilleAction.getPortefeuille();
                portefeuilleActionCollectionPortefeuilleAction.setPortefeuille(portefeuille);
                portefeuilleActionCollectionPortefeuilleAction = em.merge(portefeuilleActionCollectionPortefeuilleAction);
                if (oldPortefeuilleOfPortefeuilleActionCollectionPortefeuilleAction != null) {
                    oldPortefeuilleOfPortefeuilleActionCollectionPortefeuilleAction.getPortefeuilleActionCollection().remove(portefeuilleActionCollectionPortefeuilleAction);
                    oldPortefeuilleOfPortefeuilleActionCollectionPortefeuilleAction = em.merge(oldPortefeuilleOfPortefeuilleActionCollectionPortefeuilleAction);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPortefeuille(portefeuille.getNumPort()) != null) {
                throw new PreexistingEntityException("Portefeuille " + portefeuille + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Portefeuille portefeuille) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Portefeuille persistentPortefeuille = em.find(Portefeuille.class, portefeuille.getNumPort());
            Client emailOld = persistentPortefeuille.getEmail();
            Client emailNew = portefeuille.getEmail();
            Collection<PortefeuilleAction> portefeuilleActionCollectionOld = persistentPortefeuille.getPortefeuilleActionCollection();
            Collection<PortefeuilleAction> portefeuilleActionCollectionNew = portefeuille.getPortefeuilleActionCollection();
            List<String> illegalOrphanMessages = null;
            for (PortefeuilleAction portefeuilleActionCollectionOldPortefeuilleAction : portefeuilleActionCollectionOld) {
                if (!portefeuilleActionCollectionNew.contains(portefeuilleActionCollectionOldPortefeuilleAction)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PortefeuilleAction " + portefeuilleActionCollectionOldPortefeuilleAction + " since its portefeuille field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (emailNew != null) {
                emailNew = em.getReference(emailNew.getClass(), emailNew.getEmail());
                portefeuille.setEmail(emailNew);
            }
            Collection<PortefeuilleAction> attachedPortefeuilleActionCollectionNew = new ArrayList<PortefeuilleAction>();
            for (PortefeuilleAction portefeuilleActionCollectionNewPortefeuilleActionToAttach : portefeuilleActionCollectionNew) {
                portefeuilleActionCollectionNewPortefeuilleActionToAttach = em.getReference(portefeuilleActionCollectionNewPortefeuilleActionToAttach.getClass(), portefeuilleActionCollectionNewPortefeuilleActionToAttach.getPortefeuilleActionPK());
                attachedPortefeuilleActionCollectionNew.add(portefeuilleActionCollectionNewPortefeuilleActionToAttach);
            }
            portefeuilleActionCollectionNew = attachedPortefeuilleActionCollectionNew;
            portefeuille.setPortefeuilleActionCollection(portefeuilleActionCollectionNew);
            portefeuille = em.merge(portefeuille);
            if (emailOld != null && !emailOld.equals(emailNew)) {
                emailOld.getPortefeuilleCollection().remove(portefeuille);
                emailOld = em.merge(emailOld);
            }
            if (emailNew != null && !emailNew.equals(emailOld)) {
                emailNew.getPortefeuilleCollection().add(portefeuille);
                emailNew = em.merge(emailNew);
            }
            for (PortefeuilleAction portefeuilleActionCollectionNewPortefeuilleAction : portefeuilleActionCollectionNew) {
                if (!portefeuilleActionCollectionOld.contains(portefeuilleActionCollectionNewPortefeuilleAction)) {
                    Portefeuille oldPortefeuilleOfPortefeuilleActionCollectionNewPortefeuilleAction = portefeuilleActionCollectionNewPortefeuilleAction.getPortefeuille();
                    portefeuilleActionCollectionNewPortefeuilleAction.setPortefeuille(portefeuille);
                    portefeuilleActionCollectionNewPortefeuilleAction = em.merge(portefeuilleActionCollectionNewPortefeuilleAction);
                    if (oldPortefeuilleOfPortefeuilleActionCollectionNewPortefeuilleAction != null && !oldPortefeuilleOfPortefeuilleActionCollectionNewPortefeuilleAction.equals(portefeuille)) {
                        oldPortefeuilleOfPortefeuilleActionCollectionNewPortefeuilleAction.getPortefeuilleActionCollection().remove(portefeuilleActionCollectionNewPortefeuilleAction);
                        oldPortefeuilleOfPortefeuilleActionCollectionNewPortefeuilleAction = em.merge(oldPortefeuilleOfPortefeuilleActionCollectionNewPortefeuilleAction);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = portefeuille.getNumPort();
                if (findPortefeuille(id) == null) {
                    throw new NonexistentEntityException("The portefeuille with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Portefeuille portefeuille;
            try {
                portefeuille = em.getReference(Portefeuille.class, id);
                portefeuille.getNumPort();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The portefeuille with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<PortefeuilleAction> portefeuilleActionCollectionOrphanCheck = portefeuille.getPortefeuilleActionCollection();
            for (PortefeuilleAction portefeuilleActionCollectionOrphanCheckPortefeuilleAction : portefeuilleActionCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Portefeuille (" + portefeuille + ") cannot be destroyed since the PortefeuilleAction " + portefeuilleActionCollectionOrphanCheckPortefeuilleAction + " in its portefeuilleActionCollection field has a non-nullable portefeuille field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Client email = portefeuille.getEmail();
            if (email != null) {
                email.getPortefeuilleCollection().remove(portefeuille);
                email = em.merge(email);
            }
            em.remove(portefeuille);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Portefeuille> findPortefeuilleEntities() {
        return findPortefeuilleEntities(true, -1, -1);
    }

    public List<Portefeuille> findPortefeuilleEntities(int maxResults, int firstResult) {
        return findPortefeuilleEntities(false, maxResults, firstResult);
    }

    private List<Portefeuille> findPortefeuilleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Portefeuille.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Portefeuille findPortefeuille(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Portefeuille.class, id);
        } finally {
            em.close();
        }
    }

    public int getPortefeuilleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Portefeuille> rt = cq.from(Portefeuille.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
